package com.company;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 想模拟文件删除失败的场景，不过无论怎么样都会删除成功，奇了怪了
 */

public class P036_DeleteFileFail {

    public static void main(String[] args) {
        Random random = new Random();
        String filename = "./file/create.txt";
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(String.valueOf(random.nextInt(Integer.MAX_VALUE)));
        }
        createFileNotClose(list, filename);
        createFlgFile(filename);
        deleteFile(filename);
        deleteFlgFile(filename);
    }


    private static void createFileNotClose(List<String> list, String filename) {
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8));
            for (String s : list) {
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createFileOld(List<String> list, String filename) {
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), StandardCharsets.UTF_8))) {
            for (String s : list) {
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createFile(List<String> list, String filename) {
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get(filename))))) {
            for (String s : list) {
                bw.write(s);
                bw.newLine();
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createFlgFile(String filename) {
        String flgFilename = filename.replace(".txt", ".flg");
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get(flgFilename))))) {
            bw.write(countRows(filename));
            bw.newLine();
            bw.write(md5(filename));
            bw.newLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int countRows(String filename) {
        int ans = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(filename));
            while (br.readLine() != null) {
                ans++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ans;
    }

    private static String md5(String filename) {
        try {
            return DigestUtils.md5Hex(new FileInputStream(filename));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static void deleteFile(String filename) {
        File file = new File(filename);
        System.out.println("文件删除" + (file.delete() ? "成功" : "失败"));
    }

    private static void deleteFlgFile(String filename) {
        String flgFilename = filename.replace(".txt", ".flg");
        File file = new File(flgFilename);
        System.out.println("文件删除" + (file.delete() ? "成功" : "失败"));
    }

}
